/**
 * Created by yangh on 2016/09/18.
 */
var main = {};
var myScroll;
(function ($) {
	main.init = function () {
		main.initEvent();
	};

	main.initEvent = function() {
		$(".bind").on("click", function()
		{
			common.alert({
				width	:	"85%",
				title	:	"绑定新手机号",
				content	:	"<input type='text' id='phonenumber' maxlength='11' style='border-bottom: 1px solid #ccc' placeholder='输入新手机号' /> <button class='pullRight' style='background-color: #0195ff;color: white;	height: 20px;line-height: 20px;	padding: 0 3px;'  onclick='verificationCode(this);'>获取验证码</button><br/> <input type='text' maxlength='11' id='phonesecurity' style='border-bottom: 1px solid #ccc; margin-left: -170px;margin-top: 10px;width: 60px;' placeholder='验证码' /> ",
				dialog	:	true,
				ok:function(){
					
					var telephone = $("#phonenumber").val();
			      	var random = $("#phonesecurity").val();
			      	var telPattern =/^1[3|4|5|7|8]\d{9}$/;
			  	  	if(!telPattern.test(telephone)){
			   	  		common.alert({content:"请输入正确的手机号码"});
			   	  		return false;
			  	  	};
			  		//ajax
				 	$.ajax({
						type:"post",
						url:path+"/manage/member/telephone",
						data:{
							telephone:telephone,
							random:random
						},
						success:function(data){
							if(data.code==000){
								common.alert({
									content:'绑定成功',
									ok:function(res){
										//隐藏域加入手机号码
										$("#telephone").val(data.result);	
										$(".bandPhone").html("<span>"+data.result+"</span>");
										//window.location.href=path+"/score/cart/ordersubmit";
									}
								});
							}else{
								common.alert({content:data.msg});
							}
						}
			        })
			        //ajax end
					
				},
				cancel:function(){
					
					
				}
			})
		});

		$(".radio:first").on("click", function () {
			$(".coupon").show().next().show();
		})

		$(".mask").on("click", function () {
			$(".coupon").hide().next().hide();
		})
	};

})(jQuery);

main.init();


/**
 * 手机验证码
 */
function verificationCode (DOM) {
	
	 //验证码回调
    //by chan
    var telephone = $("#phonenumber").val();
    var telPattern =/^1[3|4|5|7|8]\d{9}$/;
  	if(!telPattern.test(telephone)){
  		common.alert({content:"请输入正确的手机号码"});
  		return false;
  	};
 	//ajax
	$.ajax({
		type:"post",
		url:path+"/message/verifycode",
		data:{
			username:telephone
		},
		success:function(data){
			if(data.code!='000'){
				common.alert({content:data.msg});
			}
		}
	})
	//ajax end
	//验证码回调结束
    $(DOM).prop("disabled", true).css("backgroundColor", "#ccc").html("59s重新获取");
    clearInterval(verificationCodeTimer);
    var verificationCodeTimer = setInterval(function () {
        if(Number($(DOM).html().replace(/[^\d]/g,"") > 0))
            $(DOM).html((Number($(DOM).html().replace(/[^\d]/g,"")) - 1) + "s重新获取");
        else
        {
            clearInterval(verificationCodeTimer);
            $(DOM).prop("disabled", false).css("backgroundColor", "#0195ff").html("获取验证码");
 
        }
    },1000);
}


